package com.zy.modules.sys.service.impl;

import com.zy.modules.sys.entity.SysUserRoleEntity;
import com.zy.modules.sys.repo.UserRoleRepository;
import com.zy.modules.sys.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;



/**
 * 用户与角色对应关系
 *
 *
 *
 * @date 2016年9月18日 上午9:45:48
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
	@Resource
	private UserRoleRepository userRoleRepository;

	@Override
	public void saveOrUpdate(Long userId, List<Long> roleIdList) {
		if(roleIdList.size() == 0){
			return ;
		}
		
		//先删除用户与角色关系
		userRoleRepository.delete(userId);
		
		//保存用户与角色关系
		List<SysUserRoleEntity> entities = new ArrayList<>(roleIdList.size());
		for (Long roleId : roleIdList) {
			SysUserRoleEntity entity = new SysUserRoleEntity();
			entity.setUserId(userId);
			entity.setRoleId(roleId);
			entities.add(entity);
		}
		userRoleRepository.save(entities);
	}

	@Override
	public List<Long> queryRoleIdList(Long userId) {
		return userRoleRepository.findByUserId(userId);
	}

	@Override
	public void delete(Long userId) {
		userRoleRepository.deleteByUserId(userId);
	}
}
